﻿@namespace Masa.Blazor
@inherits MasaComponentBase
@using Masa.Blazor.Components.TimePicker
@using static Masa.Blazor.Components.DatePicker.MDatePickerTitle
@using StyleBuilder = Masa.Blazor.Core.StyleBuilder

<MPicker TitleContent="@(GenPickerTitle())"
         ActionsContent="@ChildContent"
         Color="@(HeaderColor ?? Color)"
         Dark="@Dark"
         Elevation="@Elevation"
         Flat="@Flat"
         FullWidth="@FullWidth"
         Landscape="@Landscape"
         Light="@Light"
         Width="@Width"
         NoTitle="@NoTitle"
         Class="@GetClass()"
         Style="@GetStyle()"
         Id="@Id"
         @attributes="@Attributes">
    <KeyTransitionElement Value="@Selecting"
                          Class="@_block.Element("container").Name">
        @if (!AmPmInTitle && IsAmPm)
        {
            @GenClockAmPm()
        }

        @GenClock()
    </KeyTransitionElement>
</MPicker>

@code {

    private RenderFragment GenPickerTitle() => NoTitle
        ? null
        : __builder =>
        {
            <MTimePickerTitle AmPm="@IsAmPm"
                              AmPmReadonly="@(IsAmPm && !AmPmInTitle)"
                              Disabled="@Disabled"
                              Hour="@InputHour"
                              Minute="@InputMinute"
                              Second="@InputSecond"
                              Period="@Period"
                              Readonly="@Readonly"
                              UseSeconds="@UseSeconds"
                              Selecting="@Selecting"
                              OnSelectingUpdate="@HandleOnSelectingUpdate"
                              OnPeriodUpdate="@HandleOnPeriodUpdate">
            </MTimePickerTitle>

            void HandleOnSelectingUpdate(SelectingTime selecting)
            {
                Selecting = selecting;
            }

            async Task HandleOnPeriodUpdate(TimePeriod period)
            {
                await SetPeriodAsync(period);

                await OnPeriodUpdate.InvokeAsync(period);
            }
        };

    private RenderFragment GenClockAmPm() => __builder =>
    {
        <div class="@GetClass(_block.Element("ampm").Name, CssClassUtils.GetTextColor(Color ?? "primary"))"
             style="@CssStyleUtils.GetTextColor(Color ?? "primary")">
            @GenPickerButton(EventCallback.Factory.Create<MouseEventArgs>(this, HandleOnAmClickAsync), RenderFragments.RenderText(AmText), Period == TimePeriod.Am, Disabled || Readonly)
            @GenPickerButton(EventCallback.Factory.Create<MouseEventArgs>(this, HandleOnPmClickAsync), RenderFragments.RenderText(PmText), Period == TimePeriod.Pm, Disabled || Readonly)
        </div>
    };

    private RenderFragment GenClock() => __builder =>
    {
        var selectingHour = Selecting == SelectingTime.Hour;
        var selectingMinute = Selecting == SelectingTime.Minute;

        var allowedValues = selectingHour ? IsAllowedHourCb : (selectingMinute ? IsAllowedMinuteCb : IsAllowedSecondCb);
        Func<int, string> format = selectingHour ? (IsAmPm ? val => TimeHelper.Convert24To12(val).ToString() : val => $"{val}") : val => Pad(val);
        <MTimePickerClock AllowedValues="@allowedValues"
                          Color="@Color"
                          Dark="@Dark"
                          Disabled="@Disabled"
                          Double="@(selectingHour && !IsAmPm)"
                          Format="@format"
                          Light="@Light"
                          Max="@(selectingHour ? IsAmPm && Period == TimePeriod.Am ? 11 : 23 : 59)"
                          Min="@(selectingHour && IsAmPm && Period == TimePeriod.Pm ? 12 : 0)"
                          Readonly="@Readonly"
                          Scrollable="@Scrollable"
                          Step="@(selectingHour ? 1 : 5)"
                          Value="@(selectingHour ? InputHour : selectingMinute ? InputMinute : InputSecond)"
                          Selecting="@Selecting"
                          OnInput="@HandleOnInputAsync"
                          OnChange="@HandleOnChangeAsync">
        </MTimePickerClock>
    };

}